package com.basic.business.finance.controller;


import java.util.ArrayList;
import java.util.List;
import java.io.IOException;

import com.basic.business.finance.domain.TMoneyDetails;
import com.basic.business.finance.domain.TMoneySummaryExport;
import com.basic.business.finance.service.ITMoneyDetailsService;
import com.basic.business.finance.service.ITMoneySummaryService;
import com.basic.common.utils.StringUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.annotation.Resource;

import com.basic.common.annotation.FileExport;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.web.bind.annotation.*;
import com.basic.common.annotation.Log;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.enums.BusinessType;
import com.basic.business.finance.domain.TMoneyMain;
import com.basic.business.finance.service.ITMoneyMainService;
import com.basic.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 账单Controller
 * 
 * @author zhaoxy_jn@163.com
 */
@RestController
@RequestMapping("/business/payExcels")
public class TMoneyMainController extends BaseController
{
    @Resource
    private ITMoneyMainService tMoneyMainService;

    @Resource
    private ITMoneyDetailsService detailsService;

    @Resource
    private ITMoneySummaryService summaryService;

    /**
     * 查询账单列表
     */
    @PreAuthorize("@ss.hasPermi('business:payExcels:list')")
    @GetMapping("/list")
    public TableDataInfo list(TMoneyMain tMoneyMain)
    {
        startPage();
        List<TMoneyMain> list = tMoneyMainService.getList(tMoneyMain);
        return getDataTable(list);
    }


    @GetMapping("/getPayDetails")
    public TableDataInfo getPayDetails(TMoneyDetails details)
    {
        startPage();
        List<TMoneyDetails> list = detailsService.getList(details);
        return getDataTable(list);
    }

    /**
     * 导出账单列表
     */
    @PreAuthorize("@ss.hasPermi('business:payExcels:export')")
    @Log(title = "账单", businessType = BusinessType.EXPORT)
    @FileExport(fileName = "账单分类汇总导出.xlsx")
    @PostMapping("/export")
    public String export(@RequestBody TMoneyMain tMoneyMain) throws IOException
    {
        List<String> ids = new ArrayList<>();
        if ((MapUtils.isNotEmpty(tMoneyMain.getParams()) || StringUtils.isNotEmpty(tMoneyMain.getExcelName()) || StringUtils.isNotEmpty(tMoneyMain.getStoreCode()) || StringUtils.isNotEmpty(tMoneyMain.getStoreName()))) {
            ids = tMoneyMainService.getIds(tMoneyMain);
        }

        if (tMoneyMain.getIds() != null) {
            ids.addAll(tMoneyMain.getIds());
        }

        if (tMoneyMain.getId() != null) {
            ids.add(tMoneyMain.getId());
        }

        List<TMoneySummaryExport> list = summaryService.getExportInfo(ids);

        return EasyExcelUtils.excelExport(TMoneySummaryExport.class,"账单数据", list);
    }

    /**
     * 删除账单
     */
    @PreAuthorize("@ss.hasPermi('business:payExcels:remove')")
    @Log(title = "账单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tMoneyMainService.deleteByIds(ids));
    }


    /**
     * 账单导入
     */
    @PreAuthorize("@ss.hasPermi('business:payExcels:import')")
    @Log(title = "账单导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importCsv")
    public AjaxResult importCsv(@RequestParam("files") MultipartFile[] files, @RequestParam("rulePlatform") String rulePlatform) throws InterruptedException {

        tMoneyMainService.importCsv(files, rulePlatform);
        return AjaxResult.success();
    }

    /**
     * 重新生成汇总
     */
    @PreAuthorize("@ss.hasPermi('business:payExcels:gen')")
    @Log(title = "账单导入", businessType = BusinessType.IMPORT)
    @PostMapping("/reGenerate")
    public AjaxResult reGenerate(@RequestBody TMoneyMain moneyMain){
        summaryService.reGenerate(moneyMain);
        return null;
    }


}
